package com.pointcircle.core.web;

import java.io.Serializable;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import com.pointcircle.core.ApplicationContextHolder;
import com.pointcircle.core.JpaBaseEntity;
import com.pointcircle.core.service.PropertyService;
import com.pointcircle.core.util.RestResponseUtils;
import com.pointcircle.core.web.annotation.ApiPermissionConfig;

public interface IRemoveRest<T extends JpaBaseEntity<ID>, ID extends Serializable, 
	EV extends EntityVo<T, ID>> 
	extends IRest<T, ID, EV> {

	@PostMapping(value = "/remove")
	@Transactional
	@ApiPermissionConfig("remove")
	default RestResponse remove(@RequestBody FormVo<ID> form) {
		T entity = getRepository().findOne(form.getId());
		if(entity == null) {
			PropertyService propertyService = ApplicationContextHolder.getBean(PropertyService.class);
			throw new RestException(
				propertyService.getProperty("pointcircle.framework.message.object-not-exist")
			);
		}
		IRemoveRest<T, ID, EV> self = (IRemoveRest<T, ID, EV>) this.getSelfComponent();
		self.deleteEntity(entity);
		return RestResponseUtils.ok("删除成功");
	}
	
	void deleteEntity(T entity);
}
